-
Notifications
You must be signed in to change notification settings - Fork 764
Implement tom-select for related documents #6610
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
smithellis
wants to merge
2
commits into
mozilla:main
Choose a base branch
from
smithellis:improve-related-documents-functionality
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,116 @@ | ||
import Search from "sumo/js/search_utils"; | ||
import TomSelect from "tom-select"; | ||
|
||
import "sumo/tpl/wiki-related-doc.njk"; | ||
import "sumo/tpl/wiki-search-results.njk"; | ||
import nunjucksEnv from "sumo/js/nunjucks"; // has to be loaded after templates | ||
|
||
(function($) { | ||
var searchTimeout; | ||
var locale = $('html').attr('lang'); | ||
document.addEventListener("DOMContentLoaded", function() { | ||
const locale = document.documentElement.lang; | ||
const search = new Search(`/${locale}/search`, { w: 1, format: 'json' }); | ||
|
||
var $searchField = $('#search-related'); | ||
var $relatedDocsList = $('#related-docs-list'); | ||
var $resultsList; | ||
const relatedDocsList = document.getElementById('related-docs-list'); | ||
const searchInput = document.getElementById('search-related'); | ||
|
||
// To search for only wiki articles we pass w=1 | ||
var search = new Search('/' + locale + '/search', {w: 1, format: 'json'}); | ||
if (!searchInput || !relatedDocsList) { | ||
return; | ||
} | ||
|
||
function createResultsList() { | ||
$resultsList = $('<div />').addClass('input-dropdown'); | ||
$searchField.after($resultsList); | ||
$resultsList.css('width', $searchField.outerWidth()); | ||
$resultsList.show(); | ||
if (document.body.classList.contains('edit_metadata') || document.body.classList.contains('new')) { | ||
relatedDocsList.style.display = 'none'; | ||
} | ||
|
||
$resultsList.on('click', '[data-pk]', function() { | ||
var $this = $(this); | ||
let currentDocId = null; | ||
const documentForm = document.querySelector('form[data-document-id]'); | ||
if (documentForm) { | ||
currentDocId = documentForm.dataset.documentId; | ||
} | ||
|
||
$relatedDocsList.children('.empty-message').remove(); | ||
const tomSelect = new TomSelect(searchInput, { | ||
valueField: 'id', | ||
labelField: 'title', | ||
searchField: 'title', | ||
create: false, | ||
closeAfterSelect: true, | ||
maxItems: null, // Allow multiple selections | ||
plugins: { | ||
remove_button: { | ||
title: 'Remove this document' | ||
} | ||
}, | ||
load: function(query, callback) { | ||
if (!query.length) { | ||
return callback(); | ||
} | ||
|
||
if (!$relatedDocsList.children('[data-pk=' + $this.data('pk') + ']').length) { | ||
var context = { | ||
name: 'related_documents', | ||
search.query(query, function(data) { | ||
if (!data || !data.results) { | ||
return callback(); | ||
} | ||
|
||
const formattedResults = data.results | ||
.filter(result => result.type === 'document') | ||
.map(item => { | ||
let id = item.id; | ||
if (!id && item.url) { | ||
const match = item.url.match(/\/(\d+)\//); | ||
if (match) { | ||
id = match[1]; | ||
} | ||
} | ||
return { | ||
id: id, | ||
title: item.title, | ||
url: item.url | ||
}; | ||
}) | ||
.filter(item => item.id) | ||
.filter(item => !currentDocId || item.id != currentDocId); | ||
|
||
callback(formattedResults); | ||
}); | ||
}, | ||
onItemAdd: function(value, item) { | ||
const emptyMessage = relatedDocsList.querySelector('.empty-message'); | ||
if (emptyMessage) { | ||
emptyMessage.remove(); | ||
} | ||
// Note: The actual list item is added in render.item | ||
}, | ||
render: { | ||
option: function(item, escape) { | ||
return `<div>${escape(item.title)}</div>`; | ||
}, | ||
// Renders the selected item in the input field and triggers adding the item visually to the list below. | ||
item: function(item, escape) { | ||
const context = { | ||
name: 'related_documents', // Input field name prefix | ||
doc: { | ||
id: $this.data('pk'), | ||
title: $this.text() | ||
id: item.id, | ||
title: item.title | ||
} | ||
}; | ||
relatedDocsList.insertAdjacentHTML('beforeend', nunjucksEnv.render('wiki-related-doc.njk', context)); | ||
|
||
$relatedDocsList.append(nunjucksEnv.render('wiki-related-doc.njk', context)); | ||
} | ||
}); | ||
} | ||
|
||
function showResults(data) { | ||
if (!$resultsList) { | ||
createResultsList(); | ||
} | ||
$resultsList.html(nunjucksEnv.render('wiki-search-results.njk', data)); | ||
} | ||
|
||
function handleSearch() { | ||
var $this = $(this); | ||
if ($this.val().length === 0) { | ||
window.clearTimeout(searchTimeout); | ||
if ($resultsList) { | ||
$resultsList.html(''); | ||
$resultsList.hide(); | ||
// This div is what TomSelect displays in the input field for the selected item | ||
return `<div>${escape(item.title)}</div>`; | ||
}, | ||
no_results: function(data, escape) { | ||
const noDocsFound = typeof gettext !== 'undefined' ? gettext('No documents found') : 'No documents found'; | ||
return `<div class="no-results">${noDocsFound}</div>`; | ||
} | ||
} else if ($this.val() !== search.lastQuery) { | ||
window.clearTimeout(searchTimeout); | ||
searchTimeout = window.setTimeout(function () { | ||
search.query($this.val(), showResults); | ||
}, 200); | ||
} | ||
} | ||
|
||
$searchField.on('keyup', handleSearch); | ||
}); | ||
|
||
$searchField.on('focus', function() { | ||
if ($resultsList) { | ||
$resultsList.show(); | ||
tomSelect.on('item_remove', function(value) { | ||
const itemToRemove = relatedDocsList.querySelector(`[data-pk="${value}"]`); | ||
if (itemToRemove) { | ||
itemToRemove.remove(); | ||
} | ||
}); | ||
|
||
$searchField.on('blur', function() { | ||
if ($resultsList) { | ||
// We use a timeout to ensure that you can still click on the dropdown | ||
window.setTimeout(function() { | ||
$resultsList.hide(); | ||
}, 100); | ||
if (relatedDocsList.children.length === 0) { | ||
const noRelatedDocs = typeof gettext !== 'undefined' ? gettext('No related documents.') : 'No related documents.'; | ||
relatedDocsList.innerHTML = `<div class="empty-message">${noRelatedDocs}</div>`; | ||
} | ||
}); | ||
})(jQuery); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 8 additions & 10 deletions
18
kitsune/wiki/jinja2/wiki/includes/related_docs_widget.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
<input type="search" autocomplete="off" id="search-related" placeholder="{{ _('Search for documents...') }}"> | ||
<ul id="related-docs-list"> | ||
<select id="search-related" class="tom-select-related-docs" placeholder="{{ _('Search for documents...') }}"> | ||
{% if related_documents.count() %} | ||
{% for doc in related_documents.all() %} | ||
<li data-pk="{{ doc.pk }}"> | ||
<input type="checkbox" name="{{ name }}" value="{{ doc.pk }}" checked/> | ||
{{ doc.title }} | ||
<span data-close-type="remove" class="close-button"></span> | ||
</li> | ||
<option value="{{ doc.pk }}" selected>{{ doc.title }}</option> | ||
{% endfor %} | ||
{% else %} | ||
<li class="empty-message">{{ _('No related documents.') }}</li> | ||
{% endif %} | ||
</ul> | ||
</select> | ||
<div id="related-docs-list"> | ||
{% if not related_documents.count() %} | ||
<div class="empty-message">{{ _('No related documents.') }}</div> | ||
{% endif %} | ||
</div> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be
!==